﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOHVote.BLL;
 
using System.Web.Security;
using BOHVote.Common;
using BOHVote.Entity;

namespace BOH.ControlPanel
{
    public partial class PersonalRegister : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGovern();
                if (Session["user"] != null)
                {
                    BHUser bu = (BHUser)Session["user"];
                    if (bu.UserType == "N")
                    {
                        txtUserCode.Text = bu.UserCode;
                        txtUserCode.ReadOnly = true;
                    }
                }
            }
        }

        #region <<  绑定单位  >>
        protected void drpG_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindUnit(drpG.SelectedValue.ToString());
        }
        public void BindGovern()
        {
            DataSet dstGovern = UnitClass.getGovern();
            drpG.DataSource = dstGovern.Tables[0].DefaultView;
            drpG.DataTextField = "GovernName";
            drpG.DataValueField = "GovernID";
            drpG.DataBind();
            ListItem listitem = new ListItem("", "");
            listitem.Selected = true;
            drpG.Items.Add(listitem);
        }

        public void BindUnit(string GovernID)
        {
            DataSet dst = UnitClass.getUnitByGovern(GovernID);
            drpU.DataSource = dst;
            drpU.DataTextField = "UnitName";
            drpU.DataValueField = "UnitID";
            drpU.DataBind();
            ListItem listitem = new ListItem("", "");
            listitem.Selected = true;
            drpU.Items.Add(listitem);
        }
        #endregion

        #region <<  保存信息  >>
        protected void butSave_Click(object sender, EventArgs e)
        {
            BHUser bu = new BHUser();
            bu.UserCode = this.txtUserCode.Text;
            bu.UserName = this.txtUserName.Text.ToString();
            bu.password = common.RandomPassword();//FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text.Trim(), "MD5");
            bu.Mail = this.txtMail.Text.ToString();
            bu.UserType = "P";
            bu.GovernID = this.drpG.SelectedValue.ToString();
            bu.UnitID = this.drpU.SelectedValue.ToString();
            bu.Phone = this.txtPhone.Text.ToString();
            bu.Status = "A";
            bu.RightType = "P";
            bu.UserAccess = "P";
            string UserID = UserClass.InsertUser(bu);
            if(UserID.Trim().Length>0)
                UserClass.InsertUserAccess(UserID, "1", "A");
            Session["user"] = bu;

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>confirmInfo('" + bu.password + "');</script>");
            //Response.Redirect("ControlPanel/controlPanel.aspx", true);
        }
        #endregion
    }
}
